package util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.swing.JOptionPane;



public class ConexaoPostGree {

    private static Connection vConPostGree;

    private ConexaoPostGree() {
    }

    public static Connection getConPostGree() {
        try {
            if ((vConPostGree == null) || vConPostGree.isClosed()) {
                Class.forName("org.postgresql.Driver");
                vConPostGree = DriverManager.getConnection("jdbc:postgresql://localhost:5432/livraria", "postgres", "123456");
            }
        } catch (SQLException e) {
            JOptionPane.showMessageDialog(null, "Erro ao conectar ao banco de dados.", "Erro", JOptionPane.ERROR_MESSAGE);
        } catch (ClassNotFoundException e) {
            JOptionPane.showMessageDialog(null, "Erro ao conectar ao banco de dados.", "Erro", JOptionPane.ERROR_MESSAGE);
        }
        return vConPostGree;
    }

    public static void setConPostGree(Connection pConnection) {
        vConPostGree = pConnection;
    }
}
